
-- dependencies
require'src.MiddleClass'
require'src.Stateful'
GS = class('GS'):include(Stateful)
require'src.randomlua'
require'src.TEsound'
local anim8 = require'src.anim8'
local vector = require'hump.vector'
require'src.util'
Grid = require'jumper.grid'
Pathfinder = require'jumper.pathfinder'



-- custom
require'src.menu'
require'src.player'
require'src.newgame'
require'src.victory'
require'src.defeat'
require'src.map'
require'src.draw'
require'src.mouse'
require'src.projectiles'
require'src.features'
require'src.event'
require'src.enemy'
require'src.item'
require'src.fog'
require'src.namegen'
require'src.fx'
require'src.mapgen'
require'src.generated_map'

-- UI

arc_path = 'arc/'

require(arc_path .. 'arc')
_navi = require(arc_path..'navi')


-- levels
require'src.sleepdeck'
require'src.medbay'



function init_game()

	screen_w = love.graphics.getWidth()
	screen_h = love.graphics.getHeight()

	center_x = screen_w/2
	center_y = screen_h/2

	map_x = 0
	map_y = 0

	map_w = 32
	map_h = 32

	view_center_x = 7
	view_center_y = 5

	map_display_w = 20
	map_display_h = 15

	range_y = 9
	range_x = 13

	tile_size = 16
	scale = 4.0

	pX = map_x+view_center_x
	pY = map_y+view_center_y

	dOX = screen_w/32
	dOY = screen_h/32

	console_x = dOX+(tile_size*scale)
	console_y = dOY+(range_y*tile_size*scale)+(tile_size*scale)
	draw_con = false
	con_text = " "

	max_view_x = dOX + range_x*(tile_size*scale)
	max_view_y = dOY + range_y*(tile_size*scale)

	-- change to "basic" to enable shooting	
	mouse_mode = "basic"

	RNG = twister()

	rooms = {}

	features = {}

	events = {}

	enemies = {}

	fex = {}

	--map_fade = FX_FADE:new()

	msgs = {}
	msg_id = 0
	msg_x = 100
	msg_y = 10

	fog = {}

	game_options = {
		moody = true,
	}

	ninety_deg_rad = math.pi/2
	deg_45_rad = math.pi/4
	deg_15_rad = math.pi/12
	deg_30_rad = math.pi/6

	p_turret_rad = (tile_size*scale)/2

	NG = NameGen:new()

	prior_mouse_x = 0
	prior_mouse_y = 0

	load_images()
	load_audio()
	load_maps()
	load_player()
	load_tablet_text()
	load_event_functions()
	--init_view()
	init_proj()

	


	path_grid = {}

	love.keyboard.setKeyRepeat(0.01, 0.2)
	love.mouse.setVisible(false)






end


function clean_msg()

	msgs = {}

end



function clearLoveCallbacks()
  love.draw = nil
  love.joystickpressed = nil
  love.joystickreleased = nil
  love.keypressed = nil
  love.keyreleased = nil
  love.load = nil
  love.mousepressed = nil
  love.mousereleased = nil
  love.update = nil
end

function GS:initialize()
  super.initialize(self)
  new_game = true
  self:gotoState('Menu')
end

function love.load()
	gs = GS:new()
--	init_game()


end




function load_audio()

	help_us_aud = "audio/help_us.mp3"
	print(help_us_aud)


end

function load_images()

	--main_font = love.graphics.newFont("data/lhi.otf", 16)
	--love.graphics.setFont(main_font)

	tile_quads = {}


	anims = {}

	tb_ids = {}

	space_img = love.graphics.newImage("img/16space.png")
	floor_img = love.graphics.newImage("img/16green_patt.png")
	wall_img = love.graphics.newImage("img/metal-wall.png")
	wall_dam_img = love.graphics.newImage("img/metal-wall-dam.png")
	med_wall_img = love.graphics.newImage("img/whitetile.png")
	--man_img = love.graphics.newImage("img/top_down_m.png")
	man_img = love.graphics.newImage("img/top-down-person-c32.png")
	--man_img:setFilter("nearest","nearest")
	--woman_img = love.graphics.newImage("img/top_down_f.png")
	woman_img = love.graphics.newImage("img/top-down-person-f2c32.png")
	--woman_img:setFilter("nearest", "nearest")
	flashlight_img = love.graphics.newImage("img/flashlight-c32.png")
	aqua_ring_img = love.graphics.newImage("img/aqua_ring.png")
	bullet_img = love.graphics.newImage("img/16bullet-4.png")
	tablet_img = love.graphics.newImage("img/terminal2.png")
	tablet_img:setFilter("nearest", "nearest")
	tablet_red_img = love.graphics.newImage("img/terminal2-red.png")

	h_stim_img = love.graphics.newImage("img/health_stim.png")
	h_stim_img:setFilter("nearest", "nearest")
	metal_door_closed_img = love.graphics.newImage("img/metal_door_1.png")
	metal_door_open_img = love.graphics.newImage("img/metal_door_2.png")
	int_door_closed_img = love.graphics.newImage("img/int_door_1.png")	
	int_door_open_img = love.graphics.newImage("img/int_door_2.png")
	airlock_door_closed_img = love.graphics.newImage("img/airlock_door_1.png")
	airlock_door_open_img = love.graphics.newImage("img/airlock_door_2.png")
	mop_img = love.graphics.newImage("img/mop-c32.png")
	blood_splatter_img = love.graphics.newImage("img/16blood_splatter_2.png")

	med_analyzer_img = love.graphics.newImage("img/16colored-disk.png")

	htube1_img = love.graphics.newImage("img/hypersleep_tube_open.png")
	htube1_img:setFilter("nearest","nearest")
	htube2_img = love.graphics.newImage("img/hypersleep_tube_brown.png")
	htube2_img:setFilter("nearest","nearest")
	htube3_img = love.graphics.newImage("img/hypersleep_tube_pale.png")
	htube3_img:setFilter("nearest","nearest")

	htube_imgs = {}
	htube_imgs[1] = htube1_img
	htube_imgs[2] = htube2_img
	htube_imgs[3] = htube3_img

	herb_guy_img = love.graphics.newImage("img/herb-guy.png")
	herb_guy_img:setFilter("nearest","nearest")
	bot_guy_img = love.graphics.newImage("img/bot.png")
	bot_guy_img:setFilter("nearest", "nearest")
	preacher_bot_img = love.graphics.newImage("img/preacher-bot.png")
	preacher_bot_img:setFilter("nearest", "nearest")
	frog_img = love.graphics.newImage("img/frog-y-g.png")
	frog_img:setFilter("nearest", "nearest")
	green_bug_img = love.graphics.newImage("img/green_bug.png")
	green_bug_img:setFilter("nearest", "nearest")

	scourge_img = love.graphics.newImage("img/16scourge.png")
	scourge_img:setFilter("nearest", "nearest")
	scourge_fin_img = love.graphics.newImage("img/16scourge8.png")
	scourge_fin_img:setFilter("nearest", "nearest")

	battery_on_img = love.graphics.newImage("img/battery_on.png")
	battery_on_img:setFilter("nearest", "nearest")
	battery_off_img = love.graphics.newImage("img/battery_off.png")
	battery_off_img:setFilter("nearest", "nearest")

	yh_img = love.graphics.newImage("img/yellow_highlight.png")
	light_beam_img = love.graphics.newImage("img/light_beam5.png")
	--light_beam_img:setFilter("linear", "nearest")

	-- animation setup

	-- basic bullet

	bb_anim_img = love.graphics.newImage("img/bb_anim2.png")

	local bb_grid = anim8.newGrid(16,16,
		bb_anim_img:getWidth(), bb_anim_img:getHeight())

	bb_anim = anim8.newAnimation(bb_grid('1-8',1), 0.05, 'pauseAtEnd')


	-- scourge

	scourge_anim_img = love.graphics.newImage("img/scourge-anim.png")
	scourge_anim_img:setFilter("nearest", "nearest")

	local scg_grid = anim8.newGrid(16,16,
		scourge_anim_img:getWidth(), scourge_anim_img:getHeight())

	scg_anim = anim8.newAnimation(scg_grid('1-8',1), 0.08, 'pauseAtEnd')

	scourge_anim_frames = 8

	-- main tileset
	tileset_img = love.graphics.newImage("img/ip_tileset08.png")
	tileset_img:setFilter("nearest","nearest")

	-- space
	tile_quads[1] = love.graphics.newQuad(0*tile_size, 0*tile_size,
		tile_size, tile_size,
		tileset_img:getWidth(), tileset_img:getHeight())

	-- wall
	tile_quads[2] = love.graphics.newQuad(1*tile_size, 0*tile_size,
		tile_size, tile_size,
		tileset_img:getWidth(), tileset_img:getHeight())

	-- floor

	tile_quads[3] = love.graphics.newQuad(2*tile_size, 0*tile_size,
		tile_size, tile_size,
		tileset_img:getWidth(), tileset_img:getHeight())

	-- broken wall
	tile_quads[4] = love.graphics.newQuad(3*tile_size, 0*tile_size,
		tile_size, tile_size,
		tileset_img:getWidth(), tileset_img:getHeight())

	-- med wall
	tile_quads[5] = love.graphics.newQuad(5*tile_size, 0*tile_size,
		tile_size, tile_size,
		tileset_img:getWidth(), tileset_img:getHeight())

	-- med floor
	tile_quads[6] = love.graphics.newQuad(4*tile_size, 0*tile_size,
		tile_size, tile_size,
		tileset_img:getWidth(), tileset_img:getHeight())

	tileset_batch = love.graphics.newSpriteBatch(tileset_img, range_y*range_x)


	animations = {
		["bb"] = function(x, ani)
				ani.anim:draw(bb_anim_img,ani.x, ani.y, 0, scale, scale) end,
		["scourge"] = function(x, ani)
				ani.anim:draw(scourge_anim_img, ani.x, ani.y, 0, scale, scale) end,

	}

	build_scourge_images()


end

function build_scourge_images()

	scourge_imgs = {}

	for i = 1, scourge_anim_frames do

		scourge_imgs[i] = love.graphics.newImage("img/16scourge" .. i .. ".png")
		scourge_imgs[i]:setFilter("nearest", "nearest")

	end

end

function global_updates(dt)

	TEsound.cleanup()


end

function reset_vars()


end

